import QQMap from "../../../utils/qqmap";
Page({
  onLoad() {
    this.getLoaction();
    // this.chooseLocation()
  },
  data: {
    houseList: [],
    address: "",
  },
  async getLoaction() {
    // 获取用户当前的坐标信息
    const { latitude, longitude } = await wx.getLocation();
    console.log(latitude, longitude);
    this.getDetail(latitude, longitude);
  },

  // getLocation会调用
  // chooseLocation也会调用
  getDetail(latitude, longitude, name) {
    // 地址逆解析，传入 经纬度 >> 具体的地址信息
    if (name) {
      this.setData({
        address: name,
      });
    } else {
      QQMap.reverseGeocoder({
        location: [latitude, longitude].join(","),
        success: (res) => {
          this.setData({
            address: res.result.address,
          });
        },
      });
    }
    QQMap.search({
      keyword: "住宅小区", //搜索关键词
      location: [latitude, longitude].join(","), //设置周边搜索中心点
      page_size: 5,
      success: (res) => {
        this.setData({
          houseList: res.data.map((item) => {
            return {
              id: item.id,
              title: item.title,
              _distance: item._distance,
            };
          }),
        });
      },
    });
  },
  async chooseLocation() {
    const { latitude, longitude, name } = await wx.chooseLocation();
    console.log(latitude, longitude);
    this.getDetail(latitude, longitude, name);
  },
});
